package com.reading_recognition_system.controller;

import com.reading_recognition_system.pojo.User;
import com.reading_recognition_system.service.impl.UserServiceImpl;
import com.reading_recognition_system.util.Result;
import com.reading_recognition_system.util.StatusCode;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserServiceImpl userService;

    //登录
    @RequestMapping("/login")
    @ResponseBody
    public Result<String> login(String usercode, String password, HttpServletRequest request) {

        User user1 = userService.findUser(usercode, password);
        HttpSession session = request.getSession();
        if (user1 != null) {
            session.setAttribute("user1", user1);
            User user = (User) session.getAttribute("user1");
//            System.out.println(user.getPower());
            return new Result<String>(StatusCode.SUCCESS, "登录成功", "成功");
        } else {
            return new Result<String>(StatusCode.EXCEPTION, "账号或密码错误", "失败");
        }
    }

    //退出登录
    @RequestMapping("/logout")
    @ResponseBody
    public Result<String> logout(HttpServletRequest request) {
        HttpSession session = request.getSession();
        session.invalidate();
        return new Result<String>(StatusCode.SUCCESS, "退出成功", "成功");
    }

    //注册
    @RequestMapping("/register")
    @ResponseBody
    public Result<String> register(String usercode, String password) {
        if (usercode != "" && password != "") {
            userService.insertUser(usercode, password);
            return new Result<String>(StatusCode.SUCCESS, "注册成功", "成功");
        } else {
            return new Result<String>(StatusCode.EXCEPTION, "注册失败", "失败");
        }
    }

    //查询个人信息.
    @RequestMapping("/finduser")
    @ResponseBody
    public Result<User> finduser(int u_id) {
        User res = userService.findOne(u_id);
        return new Result<User>(StatusCode.SUCCESS, res, "成功");
    }

    //更新个人信息
    @RequestMapping("/updateuser")
    @ResponseBody
    public Result<String> updateuser(@RequestBody User user) {
        if (userService.updateUser(user) != 0) {
            return new Result<>(StatusCode.SUCCESS, "更新成功", "成功");
        } else {
            return new Result<>(StatusCode.EXCEPTION, "更新失败", "失败");
        }
    }

    //注销用户
    @RequestMapping("/deleteuser")
    @ResponseBody
    public Result<String> deleteuser(int u_id) {
        if (userService.deleteUser(u_id) != 0) {
            return new Result<>(StatusCode.SUCCESS, "注销成功", "成功");
        } else {
            return new Result<>(StatusCode.EXCEPTION, "注销失败", "失败");
        }
    }
}
